//
//  AirLineUtil.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/20.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class AirLineUtil: NSObject {
    
    // 单例
    static let shareInstance:AirLineUtil = {
        return AirLineUtil()
    }()
    
    private lazy var formatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        return formatter
    }()
    
    private lazy var cale = Calendar.current
    
    // 字符串转日期
    func str2Date(str:String) -> Int {
        guard let end = str.split(separator: "-").last?.description else {
            return 0
        }
        guard let now = formatter.string(from: Date()).split(separator: "-").last?.description else {
            return 0
        }
        return (Int(end) ?? 0) - (Int(now) ?? 0)
    }
    
    // 获取日历显示的天数
    func getShowDayCount() -> Int {
        // 当前月
        let date = Date()
        let time = formatter.string(from: date)
        let timeCount = getCurentMonthDays(time: time)
        let timeDay = cale.component(.day, from: date)
        // 当前月下一月
        let dateNext = Date(timeIntervalSinceNow: TimeInterval(24 * 60 * 60 * 30))
        let timeNext = formatter.string(from: dateNext)
        let timeNextCount = getCurentMonthDays(time: timeNext)
        
        return timeCount + timeNextCount - timeDay
    }
    
    // 仅获取年月
    func getOnlyYearMoth() -> [DateInfo] {
        // 当前月
        let date = Date()
        let time = formatter.string(from: date)
        let timeCount = getCurentMonthDays(time: time)
        let timeDate = getConvertDate(time: time,isOnlyYearMoth: true)
        let timeSeek = getMonthFirstDay(time: date)
        let timeDay = cale.component(.day, from: date)
        print("timeDate=\(timeDate) timeSeek=\(timeSeek) timeDay=\(timeDay) timeCount=\(timeCount)")
        // 当前月下一月
        let dateNext = Date(timeIntervalSinceNow: TimeInterval(24 * 60 * 60 * 31))
        let timeNext = formatter.string(from: dateNext)
        let timeNextCount = getCurentMonthDays(time: timeNext)
        let timeNextDate = getConvertDate(time: timeNext,isOnlyYearMoth: true)
        let timeNextSeek = getMonthFirstDay(time: dateNext)
        print("timeNextDate=\(timeNextDate) timeNextSeek=\(timeNextSeek) timeNextCount=\(timeNextCount)")
        // 拼装数据
        var timeInfo = DateInfo(ds:timeCount, ys: timeDate,se: timeSeek)
        timeInfo.currentDay = timeDay
        let timeNextInfo = DateInfo(ds:timeNextCount, ys: timeNextDate,se: timeNextSeek)
        return [timeInfo,timeNextInfo]
    }
    
    // 获取月的总天数
    private func getCurentMonthDays(time:String) -> Int {
        guard let date = formatter.date(from: time) else {
            return 0
        }
        let range = cale.range(of: Calendar.Component.day, in: Calendar.Component.month, for: date)
        return range!.count
    }
    
    // 获取当月第一天星期几
    public func getMonthFirstDay(time:Date) -> Int {
        let componetns = cale.dateComponents(Set<Calendar.Component>([.year,.month]), from: time)
        let date = cale.date(from: componetns)
        return cale.component(.weekday, from: date!)
    }
    
    
    // 月份
    func getDateForCount(count:Int,month:Bool = false) -> [String] {
        var temp = [String]()
        for i in 0...count {
            let time = formatter.string(from: Date(timeIntervalSinceNow: TimeInterval(24 * 60 * 60 * i)))
            let date = getConvertDate(time: time,isYear: true,isOnlyMonth:month, isOnlyWeekDay: !month)
            temp.append(date)
        }
        return temp
    }
    
    
    // MARK: 当前日期
    func getCurrentDate(isWeekDay:Bool = false) -> String {
        let time = formatter.string(from: Date())
        return getConvertDate(time: time,isYear: true,isWeekDay: isWeekDay)
    }
    
    func getCurrYear() -> String {
        let year = cale.component(.year, from: Date())
        return String(year)
    }
    
    // MARK: 转化日期
    func getConvertDate(time:String,isYear:Bool = false,isWeekDay:Bool = false,isOnlyMonth:Bool = false,isOnlyWeekDay:Bool = false,isOnlyYearMoth:Bool = false) -> String {
        guard let date = formatter.date(from: time) else {
            return ""
        }
        // 日
        let day = cale.component(.day, from: date)
        // 月
        let month = cale.component(.month, from: date)
        // 年
        let year = cale.component(.year, from: date)
        // 星期几
        let weekDay = getWeekDay(week: cale.component(.weekday, from: date))
        
        if isOnlyYearMoth {
            return "\(year)年\(String(format: "%02d", month))月"
        }
        
        if isOnlyMonth {
            return "\(String(format: "%02d", month))-\(day)"
        }
        if isOnlyWeekDay {
            return weekDay
        }
        if !isYear {
            return "\(String(format: "%02d", month))-\(day) \(weekDay)"
        }
        if isWeekDay {
            return "\(year)年\(String(format: "%02d", month))月\(day)日"
        }
        return "\(year)-\(String(format: "%02d", month))-\(String(format: "%02d", day)) \(weekDay)"
    }
    
    // 星期转换
    private func getWeekDay(week:Int) -> String {
        var result = ""
        switch week {
        case 1:
            result = "周日"
        case 2:
            result = "周一"
        case 3:
            result = "周二"
        case 4:
            result = "周三"
        case 5:
            result = "周四"
        case 6:
            result = "周五"
        case 7:
            result = "周六"
        default:
            break
        }
        return result
    }
}
